
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/physics/units/examples.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:22 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Examples &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../../../_static/sympy-notailtext-favicon.ico"/>
    <link href="examples.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="Dimensions and dimension systems" href="dimensions.html" />
    <link rel="prev" title="Philosophy behind unit systems" href="philosophy.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="dimensions.html" title="Dimensions and dimension systems"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="philosophy.html" title="Philosophy behind unit systems"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../index.html" >Physics</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="index.html" accesskey="U">Unit systems</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Examples</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="examples">
<h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
<p>In the following sections we give few examples of what can be done with this
module.</p>
<section id="dimensional-analysis">
<h2>Dimensional analysis<a class="headerlink" href="#dimensional-analysis" title="Permalink to this headline">¶</a></h2>
<p>We will start from Newton’s second law</p>
<div class="math notranslate nohighlight">
\[m a = F\]</div>
<p>where <span class="math notranslate nohighlight">\(m, a\)</span> and <span class="math notranslate nohighlight">\(F\)</span> are the mass, the acceleration and the force
respectively. Knowing the dimensions of <span class="math notranslate nohighlight">\(m\)</span> (<span class="math notranslate nohighlight">\(M\)</span>) and <span class="math notranslate nohighlight">\(a\)</span>
(<span class="math notranslate nohighlight">\(L T^{-2}\)</span>), we will determine the dimension of <span class="math notranslate nohighlight">\(F\)</span>; obviously we
will find that it is a force: <span class="math notranslate nohighlight">\(M L T^{-2}\)</span>.</p>
<p>From there we will use the expression of the gravitational force between the
particle of mass <span class="math notranslate nohighlight">\(m\)</span> and the body of mass <span class="math notranslate nohighlight">\(M\)</span>, at a distance
<span class="math notranslate nohighlight">\(r\)</span></p>
<div class="math notranslate nohighlight">
\[F = \frac{G m M}{r^2}\]</div>
<p>to determine the dimension of the Newton’s constant <span class="math notranslate nohighlight">\(G\)</span>. The result
should be <span class="math notranslate nohighlight">\(L^3 M^{-1} T^{-2}\)</span>.</p>
<blockquote>
<div><div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units.systems</span> <span class="kn">import</span> <span class="n">SI</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units</span> <span class="kn">import</span> <span class="n">length</span><span class="p">,</span> <span class="n">mass</span><span class="p">,</span> <span class="n">acceleration</span><span class="p">,</span> <span class="n">force</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units</span> <span class="kn">import</span> <span class="n">gravitational_constant</span> <span class="k">as</span> <span class="n">G</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units.systems.si</span> <span class="kn">import</span> <span class="n">dimsys_SI</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F</span> <span class="o">=</span> <span class="n">mass</span><span class="o">*</span><span class="n">acceleration</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F</span>
<span class="go">Dimension(acceleration*mass)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dimsys_SI</span><span class="o">.</span><span class="n">get_dimensional_dependencies</span><span class="p">(</span><span class="n">F</span><span class="p">)</span>
<span class="go">{&#39;length&#39;: 1, &#39;mass&#39;: 1, &#39;time&#39;: -2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dimsys_SI</span><span class="o">.</span><span class="n">get_dimensional_dependencies</span><span class="p">(</span><span class="n">force</span><span class="p">)</span>
<span class="go">{&#39;length&#39;: 1, &#39;mass&#39;: 1, &#39;time&#39;: -2}</span>
</pre></div>
</div>
<p>Dimensions cannot compared directly, even if in the SI convention they are
the same:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">F</span> <span class="o">==</span> <span class="n">force</span>
<span class="go">False</span>
</pre></div>
</div>
<p>Dimension system objects provide a way to test the equivalence of
dimensions:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dimsys_SI</span><span class="o">.</span><span class="n">equivalent_dims</span><span class="p">(</span><span class="n">F</span><span class="p">,</span> <span class="n">force</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m1</span><span class="p">,</span> <span class="n">m2</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;m1 m2 r&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">grav_eq</span> <span class="o">=</span> <span class="n">G</span> <span class="o">*</span> <span class="n">m1</span> <span class="o">*</span> <span class="n">m2</span> <span class="o">/</span> <span class="n">r</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F2</span> <span class="o">=</span> <span class="n">grav_eq</span><span class="o">.</span><span class="n">subs</span><span class="p">({</span><span class="n">m1</span><span class="p">:</span> <span class="n">mass</span><span class="p">,</span> <span class="n">m2</span><span class="p">:</span> <span class="n">mass</span><span class="p">,</span> <span class="n">r</span><span class="p">:</span> <span class="n">length</span><span class="p">,</span> <span class="n">G</span><span class="p">:</span> <span class="n">G</span><span class="o">.</span><span class="n">dimension</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F2</span>  
<span class="go">Dimension(mass*length*time**-2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F2</span><span class="o">.</span><span class="n">get_dimensional_dependencies</span><span class="p">()</span>  
<span class="go">{&#39;length&#39;: 1, &#39;mass&#39;: 1, &#39;time&#39;: -2}</span>
</pre></div>
</div>
</div></blockquote>
<p>Note that one should first solve the equation, and then substitute with the
dimensions.</p>
</section>
<section id="equation-with-quantities">
<h2>Equation with quantities<a class="headerlink" href="#equation-with-quantities" title="Permalink to this headline">¶</a></h2>
<p>Using Kepler’s third law</p>
<div class="math notranslate nohighlight">
\[\frac{T^2}{a^3} = \frac{4 \pi^2}{GM}\]</div>
<p>we can find the Venus orbital period using the known values for the other
variables (taken from Wikipedia). The result should be 224.701 days.</p>
<blockquote>
<div><div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">solve</span><span class="p">,</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">pi</span><span class="p">,</span> <span class="n">Eq</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units</span> <span class="kn">import</span> <span class="n">Quantity</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">mass</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units</span> <span class="kn">import</span> <span class="n">day</span><span class="p">,</span> <span class="n">gravitational_constant</span> <span class="k">as</span> <span class="n">G</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units</span> <span class="kn">import</span> <span class="n">meter</span><span class="p">,</span> <span class="n">kilogram</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">T</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s2">&quot;T&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Quantity</span><span class="p">(</span><span class="s2">&quot;venus_a&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Specify the dimension and scale in SI units:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">SI</span><span class="o">.</span><span class="n">set_quantity_dimension</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">length</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">SI</span><span class="o">.</span><span class="n">set_quantity_scale_factor</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mf">108208000e3</span><span class="o">*</span><span class="n">meter</span><span class="p">)</span>
</pre></div>
</div>
<p>Add the solar mass as quantity:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Quantity</span><span class="p">(</span><span class="s2">&quot;solar_mass&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">SI</span><span class="o">.</span><span class="n">set_quantity_dimension</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="n">mass</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">SI</span><span class="o">.</span><span class="n">set_quantity_scale_factor</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="mf">1.9891e30</span><span class="o">*</span><span class="n">kilogram</span><span class="p">)</span>
</pre></div>
</div>
<p>Now Kepler’s law:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">Eq</span><span class="p">(</span><span class="n">T</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="n">a</span><span class="o">**</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="o">*</span><span class="n">pi</span><span class="o">**</span><span class="mi">2</span> <span class="o">/</span> <span class="n">G</span> <span class="o">/</span> <span class="n">M</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span>
<span class="go">Eq(T**2/venus_a**3, 4*pi**2/(gravitational_constant*solar_mass))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">eq</span><span class="p">,</span> <span class="n">T</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">q</span>
<span class="go">2*pi*venus_a**(3/2)/(sqrt(gravitational_constant)*sqrt(solar_mass))</span>
</pre></div>
</div>
</div></blockquote>
<p>To convert to days, use the <code class="docutils literal notranslate"><span class="pre">convert_to</span></code> function (and possibly approximate
the outcoming result):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.physics.units</span> <span class="kn">import</span> <span class="n">convert_to</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">convert_to</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">day</span><span class="p">)</span>
<span class="go">71.5112118495813*pi*day</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">convert_to</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">day</span><span class="p">)</span><span class="o">.</span><span class="n">n</span><span class="p">()</span>
<span class="go">224.659097795948*day</span>
</pre></div>
</div>
<p>We could also have the solar mass and the day as units coming from the
astrophysical system, but we wanted to show how to create a unit that one needs.</p>
<p>We can see in this example that intermediate dimensions can be ill-defined,
such as sqrt(G), but one should check that the final result - when all
dimensions are combined - is well defined.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Examples</a><ul>
<li><a class="reference internal" href="#dimensional-analysis">Dimensional analysis</a></li>
<li><a class="reference internal" href="#equation-with-quantities">Equation with quantities</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="philosophy.html"
                        title="previous chapter">Philosophy behind unit systems</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="dimensions.html"
                        title="next chapter">Dimensions and dimension systems</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/modules/physics/units/examples.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="dimensions.html" title="Dimensions and dimension systems"
             >next</a> |</li>
        <li class="right" >
          <a href="philosophy.html" title="Philosophy behind unit systems"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../index.html" >Physics</a> &#187;</li>
          <li class="nav-item nav-item-3"><a href="index.html" >Unit systems</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Examples</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/physics/units/examples.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:23 GMT -->
</html>